import request from '@/utils/request'

// ==================== 领料出库相关接口 ====================

// 获取仓库列表
export function getWarehouseList(query) {
  return request({
    url: '/ck/warehouse/list',
    method: 'get',
    params: query
  })
}

// 获取可用物料列表（根据任务ID）
export function getAvailableMaterials(taskId) {
  return request({
    url: `/ck/material/available/${taskId}`,
    method: 'get'
  })
}

// 获取物料库存信息
export function getMaterialStock(materialCode, warehouseId) {
  return request({
    url: '/ck/material/stock',
    method: 'get',
    params: {
      materialCode,
      warehouseId
    }
  })
}

// 获取物料批次信息
export function getMaterialBatches(materialCode, warehouseId) {
  return request({
    url: '/ck/material/batches',
    method: 'get',
    params: {
      materialCode,
      warehouseId
    }
  })
}

// 根据物料编码查询批次库存信息（用于派工时批次选择）
export function getBatchStockByItemCode(itemCode) {
  return request({
    url: `/ldzl-ck/inventory/batchStock/${itemCode}`,
    method: 'get'
  })
}

// 生成出库单号
export function generateOutboundCode() {
  return request({
    url: '/ck/outbound/generateCode',
    method: 'get'
  })
}

// 提交领料出库单
export function submitOutbound(data) {
  return request({
    url: '/ck/outbound/submit',
    method: 'post',
    data: data
  })
}

// 保存领料出库草稿
export function saveOutboundDraft(data) {
  return request({
    url: '/ck/outbound/draft',
    method: 'post',
    data: data
  })
}

// 获取领料出库单详情
export function getOutboundDetail(outboundId) {
  return request({
    url: `/ck/outbound/${outboundId}`,
    method: 'get'
  })
}

// 获取领料出库单列表
export function getOutboundList(query) {
  return request({
    url: '/ck/outbound/list',
    method: 'get',
    params: query
  })
}

// ==================== 退料入库相关接口 ====================

// 获取已使用物料列表（根据任务ID）
export function getUsedMaterials(taskId) {
  return request({
    url: `/ck/material/used/${taskId}`,
    method: 'get'
  })
}

// 获取可退料数量
export function getReturnableQty(taskId, materialCode) {
  return request({
    url: '/ck/material/returnable',
    method: 'get',
    params: {
      taskId,
      materialCode
    }
  })
}

// 生成退料单号
export function generateReturnCode() {
  return request({
    url: '/ck/return/generateCode',
    method: 'get'
  })
}

// 提交退料入库单
export function submitReturn(data) {
  return request({
    url: '/ck/return/submit',
    method: 'post',
    data: data
  })
}

// 保存退料入库草稿
export function saveReturnDraft(data) {
  return request({
    url: '/ck/return/draft',
    method: 'post',
    data: data
  })
}

// 获取退料入库单详情
export function getReturnDetail(returnId) {
  return request({
    url: `/ck/return/${returnId}`,
    method: 'get'
  })
}

// 获取退料入库单列表
export function getReturnList(query) {
  return request({
    url: '/ck/return/list',
    method: 'get',
    params: query
  })
}

// ==================== 通用接口 ====================

// 获取物料基础信息
export function getMaterialInfo(materialCode) {
  return request({
    url: `/ck/material/info/${materialCode}`,
    method: 'get'
  })
}

// 获取物料需求清单（根据任务ID）
export function getMaterialRequirements(taskId) {
  return request({
    url: `/ck/material/requirements/${taskId}`,
    method: 'get'
  })
}

// 检查库存是否充足
export function checkStockSufficient(materialCode, warehouseId, requiredQty) {
  return request({
    url: '/ck/material/checkStock',
    method: 'post',
    data: {
      materialCode,
      warehouseId,
      requiredQty
    }
  })
}

// 获取物料出入库记录
export function getMaterialTransactionHistory(materialCode, startDate, endDate) {
  return request({
    url: '/ck/material/transactions',
    method: 'get',
    params: {
      materialCode,
      startDate,
      endDate
    }
  })
}

// 获取任务相关的物料出入库汇总
export function getTaskMaterialSummary(taskId) {
  return request({
    url: `/ck/material/taskSummary/${taskId}`,
    method: 'get'
  })
}

// ==================== 审批相关接口 ====================

// 提交审批
export function submitForApproval(type, id) {
  return request({
    url: `/ck/${type}/submitApproval`,
    method: 'post',
    data: { id }
  })
}

// 审批通过
export function approveDocument(type, id, approvalComment) {
  return request({
    url: `/ck/${type}/approve`,
    method: 'post',
    data: {
      id,
      approvalComment
    }
  })
}

// 审批拒绝
export function rejectDocument(type, id, rejectReason) {
  return request({
    url: `/ck/${type}/reject`,
    method: 'post',
    data: {
      id,
      rejectReason
    }
  })
}

// ==================== 打印相关接口 ====================

// 获取出库单打印数据
export function getOutboundPrintData(outboundId) {
  return request({
    url: `/ck/outbound/printData/${outboundId}`,
    method: 'get'
  })
}

// 获取退料单打印数据
export function getReturnPrintData(returnId) {
  return request({
    url: `/ck/return/printData/${returnId}`,
    method: 'get'
  })
}

// ==================== 统计分析接口 ====================

// 获取物料出库统计
export function getOutboundStatistics(query) {
  return request({
    url: '/ck/statistics/outbound',
    method: 'get',
    params: query
  })
}

// 获取物料退料统计
export function getReturnStatistics(query) {
  return request({
    url: '/ck/statistics/return',
    method: 'get',
    params: query
  })
}

// 获取物料周转率分析
export function getMaterialTurnoverAnalysis(query) {
  return request({
    url: '/ck/statistics/turnover',
    method: 'get',
    params: query
  })
}

// 查询物料列表
export function listMaterial(query) {
  return request({
    url: '/ck/purchase/findAll_wlgl/',
    method: 'post',
    params: query
  })
}

// 根据仓库ID查询物料库存
export function listMaterialByWarehouse(warehouseId) {
  return request({
    url: '/ck/inventory/findAll/',
    method: 'get',
    params: { warehouse_id: warehouseId }
  })
}

// 查询物料详细
export function getMaterial(materialId) {
  return request({
    url: '/ck/purchase/findAll_wlgl/',
    method: 'post',
    params: { material_id: materialId }
  })
}
